<?php

namespace app\common\model\banxi\course;

use app\common\model\common\BaseModel;
use think\exception\ValidateException;
use think\model\concern\SoftDelete;


/*
 * 课程小节
 * */

class CourseSection extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_course_section';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [

    ];
    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'courseId' => 'string',
        'sortNum' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                         ｜
    //-----------------------------------------------------------------------------------


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    /**
     * 根据课程ID获取课程小节列表
     *
     * @param int $courseId 课程ID
     * @return array
     */
    public function listByCourse($courseId)
    {
        return $this->where('courseId', $courseId)
            ->order('sortNum', 'asc')
            ->select()
            ->toArray();
    }

    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->where($where)
            ->order('sortNum', 'asc')
            ->paginate([
                'page' => $page,
                'list_rows' => $limit
            ]);
    }

    public function getInfo(array $where)
    {
        return $this->where($where)->find();
    }

    public function saveOrUpdate(array $data)
    {
        $id = $data['id'] ?? null;
        if ($id) {
            $info = $this->where('id', $id)->find();
            if (!$info) {
                throw new ValidateException('记录不存在');
            }
            $info->save($data);
        } else {
            $this->save($data);
        }
        return true;
    }

    public function del($ids)
    {
        return self::destroy(function ($query) use ($ids) {
            $query->whereIn('id', $ids);
        });
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

}